package cn.chia.back.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import cn.chia.pojo.Orders;
import cn.chia.pojo.Ordertime;
import cn.chia.service.OrderService;
import cn.chia.service.OrdertimeService;
import cn.chia.service.SenderService;
import cn.chia.utils.BuildLogFile;
import cn.chia.utils.OnCustomerService;
/*
 * 于辉  后台订单Action
 */
@Controller
public class OrderAction{
	private OrderService orderService;            //订单Service
	private OrdertimeService ordertimeService;    //订单时间Service
	private SenderService senderService;          //配送人Service
	private Orders order;                         //订单对象
	private int id;                               //订单id
	private int senderid;                         //发货人id
	private int state;                            //订单状态
	private int inmanage;                         //订单正在被处理情况，1表示正在被处理，0表示未被处理或已处理完成
	private boolean cstixing;                     //控制多客服消息提醒
	private List<?> list;                         //订单集合
	private List<?> senderlist;                   //发货人集合
	private Set<?> set;                           //某条订单所含商品的集合
    private int flagg;
    
	private int pageSize = 5;
	private int pageNo = 1;
	private String keyword="";
	private String column="orderNumber";
	private Integer count;
	
	private List<Orders> orderList;
	
	
	private double yingfumoney;
	private String yingfureason;
	
	@SuppressWarnings("unchecked")
	public String orderList() throws Exception{
		Map<String, Object> map = orderService.map(pageNo, pageSize, keyword, column);
		orderList = (List<Orders>) map.get("allOrders");
		count = (Integer) map.get("count");
		return "success";
	}
	
	
	/**
	 * 于辉  查找所有订单
	 */
	@SuppressWarnings("unchecked")
	public String list() throws Exception{
		Map<String, Object> map = orderService.map(pageNo, pageSize, keyword, column);
		list = (List<Orders>) map.get("allOrders");
		count = (Integer) map.get("count");
		System.out.println("count:订单中的记录数: " +count);
		return "success";
		
	}
	
	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getPageNo() {
		return pageNo;
	}


	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}


	/**
	 * 于辉  根据id查找订单
	 */
	public String viewbyid() throws Exception{
		//某一条订单
		order = orderService.findById(id);
		//将这条订单的商品明细放到set集合中
		set = order.getOrderitemses();
		//查找所有的送货人
		senderlist = senderService.findall();
		return "success";
	}
	
	/**
	 * 于辉  根据id查找订单
	 */
	public String payviewbyid() throws Exception{
		order = orderService.findById(id);
		set = order.getOrderitemses();
		return "success";
	}
	
	/**
	 * 于辉  修改订单状态
	 */
	public String updatestate() throws Exception{
		//退款后的操作，4代表示已退款
		if(state==4){
			order = orderService.findById(id);
			order.setState("已退款");
			order.setYingfumoney(yingfumoney);
			order.setYingfureason(yingfureason);
			Ordertime ot = ordertimeService.findById(order.getOrdertime().getOrdernumber());
			ot.setRefundedtime(new Date());
			ordertimeService.update(ot);
			//表示此订单未被处理货处理完成
			order.setInmanage(0);
		}
		orderService.updateFront(order);
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"将订单号为"+order.getOrderNumber()+"的订单状态修改为已退款。\r\n");
		flagg = 10;
		return "success";
	}
	
	/**
	 * 于辉  查看7种状态下的订单
	 */
	@SuppressWarnings("unchecked")
	public String listbystate() throws Exception{
		if(state==1){list = orderService.findOrdersByStateBack("未付款");}
		if(state==2){list = orderService.findOrdersByStateBack("已付款");}
		if(state==3){list = orderService.findOrdersByStateBack("申请退款");}
		if(state==4){list = orderService.findOrdersByStateBack("已退款");}
		if(state==5){list = orderService.findOrdersByStateBack("已发货");}
		if(state==6){list = orderService.findOrdersByStateBack("交易完成");}
		if(state==7){list = orderService.findOrdersByStateBack("已取消");}
		
		Map<String, Object> map = orderService.map(pageNo, pageSize, keyword, column);
		orderList = (List<Orders>) map.get("allOrders");
		count = (Integer) map.get("count");
		
		return "success";
	}
	
	/**
	 * 于辉  修改订单是否正在被处理状态
	 */
	public String updateInmanage() throws Exception{
		order = orderService.findById(id);
		//inmanage为1时正在被处理
			if(inmanage == 1){
				//判断当前客服和正在处理客服是否为同一人
				System.out.println(OnCustomerService.customerService().getCsid()+"//////////////////");
				System.out.println(order.getCustomerservices().getCsid()+".....................");
				if(order.getCustomerservices().getCsid().equals(OnCustomerService.customerService().getCsid())){
					order.setInmanage(0);
					order.setCustomerservices(null);
					orderService.update(order);
					cstixing = false;
				}else{
					cstixing = true;
				}
			//否则未被处理或处理完成
			}else{
				order.setInmanage(1);
				order.setCustomerservices(OnCustomerService.customerService());
				orderService.update(order);
			}
		if(flagg==0){list = orderService.findall();}
		if(flagg==1){list = orderService.findOrdersByStateBack("未付款");}
		if(flagg==2){list = orderService.findOrdersByStateBack("已付款");}
		if(flagg==3){list = orderService.findOrdersByStateBack("申请退款");}
		if(flagg==4){list = orderService.findOrdersByStateBack("已退款");}
		if(flagg==5){list = orderService.findOrdersByStateBack("已发货");}
		if(flagg==6){list = orderService.findOrdersByStateBack("交易完成");}
		if(flagg==7){list = orderService.findOrdersByStateBack("已取消");}
		if(flagg==10){list = orderService.findOverdueOrders();}
		return "success";
	}
	
	/*
	 * 于辉   查看过期拼单的订单
	 */
	public String overdueorders() throws Exception{
		list = orderService.findOverdueOrders();
		return "success";
	}
	
	/*
	 * 于辉   以下为各属性的get、set方法
	 */
	public Orders getOrder() {
		return order;
	}
	public void setOrder(Orders order) {
		this.order = order;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public List<?> getList() {
		return list;
	}
	public void setList(List<?> list) {
		this.list = list;
	}
	public Set<?> getSet() {
		return set;
	}
	public void setSet(Set<?> set) {
		this.set = set;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public List<?> getSenderlist() {
		return senderlist;
	}
	public void setSenderlist(List<?> senderlist) {
		this.senderlist = senderlist;
	}
	public int getSenderid() {
		return senderid;
	}
	public void setSenderid(int senderid) {
		this.senderid = senderid;
	}
	public int getInmanage() {
		return inmanage;
	}
	public void setInmanage(int inmanage) {
		this.inmanage = inmanage;
	}
	public boolean isCstixing() {
		return cstixing;
	}
	public void setCstixing(boolean cstixing) {
		this.cstixing = cstixing;
	}

	public int getFlagg() {
		return flagg;
	}


	public void setFlagg(int flagg) {
		this.flagg = flagg;
	}


	public double getYingfumoney() {
		return yingfumoney;
	}


	public void setYingfumoney(double yingfumoney) {
		this.yingfumoney = yingfumoney;
	}


	public String getYingfureason() {
		return yingfureason;
	}


	public void setYingfureason(String yingfureason) {
		this.yingfureason = yingfureason;
	}

	

	public String getKeyword() {
		return keyword;
	}


	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}


	public String getColumn() {
		return column;
	}


	public void setColumn(String column) {
		this.column = column;
	}


	public List<Orders> getOrderList() {
		return orderList;
	}


	public void setOrderList(List<Orders> orderList) {
		this.orderList = orderList;
	}


	public Integer getCount() {
		return count;
	}


	public void setCount(Integer count) {
		this.count = count;
	}


	@Resource(name="orderServiceImpl")
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	@Resource(name="ordertimeServiceImpl")
	public void setOrdertimeService(OrdertimeService ordertimeService) {
		this.ordertimeService = ordertimeService;
	}
	@Resource(name="senderServiceImpl")
	public void setSenderService(SenderService senderService) {
		this.senderService = senderService;
	}
}
